#include<iostream>
#include<string>
#include<vector>
using namespace std;
int count(string& s)
{
    int n = s.size();
    if (n < 3) return 0;

    int total = n * (n + 1) / 2 - n;

    int invalid = n;

    bool all_same = true;
    for (int i = 1; i < n; i++)
    {
        if (s[i] != s[0])
        {
            all_same = false;
            break;
        }
    }

    if (all_same) return 0;

    int same1 = 0;
    if (s[0] == s.back())
    {
        int left = 0, right = n - 1;
        while (left < n && s[left] == s[0]) left++;
        while (right >= 0 && s[right] == s[0]) right--;
        if (left > right)
        {
            same1 = (left - 1) * (n - right) - 1;
        }
        else
        {
            same1 = left * (n - 1 - right);
        }
    }

    int cur = 1;
    while (cur < n && s[cur] == s[0]) cur++;
    int cur2 = 1;
    while (cur2 < n && s[n - 1 - cur2] == s.back()) cur2++;

    same1 += cur + cur2 - 2;
    invalid += same1;
    return total - invalid;
}
int main()
{
    int T;
    cin >> T;
    while (T--)
    {
        string s;
        cin >> s;
        cout << count(s) << endl;
    }
    return 0;
}

/*int main()
{
    int T;
    cin >> T;
    while (T--)
    {
        int N, M, q;
        cin >> N >> M >> q;
        vector<vector<int>> ret(N + 1, vector<int>(M + 1, 0));
        while (q--)
        {
            string s1, s2;
            int x;
            cin >> s1 >> s2 >> x;
            if (s1 == "flame" && s2 == "row")
            {
                for (int j = 1; j <= M; j++)
                {
                    ret[x][j] += 1;
                }
            }
            else if (s1 == "flame" && s2 == "column")
            {
                for (int i = 1; i <= N; i++)
                {
                    ret[i][x] += 1;
                }
            }
            else if (s1 == "water" && s2 == "row")
            {
                for (int j = 1; j <= M; j++)
                {
                    ret[x][j] -= 1;
                }
            }
            else if (s1 == "water" && s2 == "column")
            {
                for (int i = 1; i <= N; i++)
                {
                    ret[i][x] -= 1;
                }
            }
        }

    }
    int sum = 0;
    for (int i = 1; i <= N; i++)
    {
        for (int j = 1; j <= M; j++)
        {
            if (ret[i][j] <= 0)
                sum++;
        }
    }
    cout << sum << endl;

    return 0;
}*/

/*vector<int> square(vector<int>& num)
{
    vector<int> res(num.size() * 2, 0);
    for (int i = 0; i < num.size(); i++)
    {
        for (int j = 0; j < num.size(); j++)
        {
            res[i + j] += num[i] * num[j];
        }
    }

    int carry = 0;
    for (int i = 0; i < res.size(); i++)
    {
        int sum = res[i] + carry;
        res[i] = sum % 10;
        carry = sum / 10;
    }

    while (res.back() == 0)
    {
        res.pop_back();
    }

    return res;
}

vector<int> fac(int n)
{
    vector<int> res = { 1 };
    for (int x = 2; x <= n; x++)
    {
        int carry = 0;
        for (auto& d : res)
        {
            int tmp = d * x + carry;
            d = tmp % 10;
            carry = tmp / 10;
        }

        while (carry)
        {
            res.push_back(carry % 10);
            carry /= 10;
        }
    }
    reverse(res.begin(), res.end());
    return res;
}
int main()
{
    int n;
    cin >> n;
    vector<int> arr;
    while (n--)
    {
        int N, M;
        cin >> N >> M;
        vector<int> ret = fac(N);
        vector<int> cur = square(ret);
        int count = 0;
        for (int i = cur.size() - 1; i >= 0; i--)
        {
            if (cur[i] == 0) count++;
            else
            {
                if (count == M)
                    arr.push_back(1);
                else
                {
                    arr.push_back(2);
                }
                break;

            }
        }

    }

    for (int i = 0; i < arr.size(); i++)
    {
        if (arr[i] == 1 && i != arr.size() - 1)
            cout << "YES" << endl;
        else if (arr[i] == 1)
            cout << "YES";
        else if (arr[i] == 2 && i != arr.size() - 1)
            cout << "NO" << endl;
        else if (arr[i] == 2)
            cout << "NO";
    }
    return 0;
}*/
/*int main()
{
    int n;
    cin >> n;
    vector<int> ret;
    while (n--)
    {
        int N, M, K, t;
        cin >> N >> M >> K >> t;
        int cur = 0;
        if (t < N)
        {
            cur = N / 10;
            ret.push_back(cur);
            break;
        }
        while (t - N >= 0)
        {
            t -= N;
            K += K;
            if (K >= M)
            {
                cur++;
                K -= M;
            }

        }

        ret.push_back(cur);
    }

    for (int i = 0; i < ret.size(); i++)
    {
        if (i < ret.size() - 1)
            cout << ret[i] << endl;
        else
            cout << ret[i];
    }
    return 0;
}*/

/*3cc?wfea
viswcgh@k - x2
ddajyw + 7
zav5t / qr
kwojsz$jdcr3
p!c0f#fr
xcuwroaj<smgl + 5
    55qldrkv58btwt>
    qb5tahzd ^ 
    wf ? boos2
    oyoweibg00hfp * 
    ztepj ^ tz ^ sqiew1
    8efrpw#y
    4#qvgd2 @
    c3wyhu$j
    x * !fhtat ^ -5
    di>fch<e>e5
    sdogvki(i > iszujfu0vl5uj*/

/*int main()
{
    long long count1= 0,count2 = 0,count = 0;
    for (int i = 10000; i <= 99999; i++)
    {
        string s = to_string(i);
        int sum1 = 0, sum2 = 0;
        for (int j = 0; j < s.size(); j++)
        {
            sum1 += s[j] - '0';
            sum2 += s[j] - '0';
        }

        if (sum1 % 2 != 0)
        {
            count1++;
        }
        else
        {
            count2++;
        }
    }
    count = count1 * count2;
    cout << count << endl;
    return 0;
}*/